var form = layui.form;
var table = layui.table;
layer = parent.layer === undefined ? layui.layer : top.layer,
    $ = layui.jquery;
layui.table.render({
    elem: '#linkList',
    url : '/user/list',
    page : true,
    cellMinWidth : 95,
    method: 'post',
    request: {
        pageName: 'page'
        ,limitName: 'limit'
    },
    parseData: function(res){
        //console.log(res.data.totalRec);
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.data.totalRec,
            "data": res.data.recList
        };
    },
    limit : 5,
    limits : [5,10,15,20],
    id : "linkListTab",
    cols : [[
        {type: "numbers", title: '编号',fixed:"left", width:50},
        {type: "checkbox", fixed:"left", width:50},
        {field: 'show_name', title: '用户名称', width:130},
        {field: 'login_name', title: '登录名称',width:130},
        {field: 'create_time', title: '创建时间',width:280, align:'center'},
        {field: 'state', title: '状态', align:'center',width:200,minWidth:	80,templet:function(){
                return '正常';
            }},
        {field: 'type', title: '用户类型',width:200, align:'center',templet:function(t){
                //console.log("类型"+t.type);
                if(t.type==1){
                    return '非员工';
                }else{
                    return '员工';
                }
            }},
        {title: '操作', width:280,fixed:"right",align:"center", toolbar: '#userList'}
    ]]
});
function insert() {
    layer.open({
        type : 2,
        title : '添加用户',
        content : '/user/toAdd',
        shade : 0.5,
        area : ["500px","500px"],
        maxmin : true,
        end: function(){
            table.reload('linkListTab');
        }
    });
}
$('#searchTransport').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
});
var active ={
    reload: function(){
        var demoReload = $('#demoReload');
        //执行重载
        layui.table.reload('linkListTab', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            ,where: {
                name: demoReload.val(),
                deliveryCode: ''
            }
        }, 'data');
    }
};
function delAll(){
    var checkStatus = layui.table.checkStatus('linkListTab'),
        data = checkStatus.data,
        roleId = [];
    if (data.length > 0) {
        for (var i in data) {
            roleId.push(data[i].id);
        }
        console.log(roleId.join(','));
        layer.confirm('确定删除选中的用户？', {icon: 3, title: '提示信息'}, function (index) {
            $.post("/user/toDelete", {
                id: roleId.join(',')  //将需要删除的newsId作为参数传入
            }, function (data) {
                layer.msg("用户删除成功！", {icon: 2, time: 500});
                layui.table.reload('linkListTab');
                layer.close(index);
                window.location.reload();
            })
        })
    } else {
        layer.msg("请选择需要删除的用户");
    }
}
layui.use('table', function() {
    //监听表格复选框选择
    table.on('checkbox(demo)', function(obj) {
        //console.log(obj)
    });
    //监听工具条
    table.on('tool(demo)', function(obj) {
        var data = obj.data;
        if (obj.event === 'tableUpdate') {
            layer.msg('name：' + data.show_name + ' 的更新操作');
            window.UpdateId=data.id;
            layer.open({
                type : 2,
                title : '客户信息',
                content : "/user/toEdit?id="+data.id,
                shade : 0.5,
                area : ["500px","500px"],
                maxmin : true,
                end: function(){
                    table.reload('linkListTab');
                }
            });
        }else if (obj.event === 'grant') {
            console.log("分配角色");
            var index = layer.open({
                title : "分配角色",
                type : 2,
                area : ["500px","500px"],
                content : "/user/toGrant?id="+data.id,
                end: function(){
                    table.reload('linkListTab');
                }
            })
        }
        else if (obj.event === 'tableDelete') {
            layer.confirm('真的删除行么', function(res) {
                obj.del();
                $.ajax({
                    type : "post",
                    url : "/user/delete?id=" + data.id,
                    dataType : "json",
                    success:function(res){
                        console.log("res.code:"+res.code);
                        if(res.code === 0){
                            top.layer.msg("操作成功！");
                            setTimeout(function(){
                                window.location.reload();
                            },1000);
                        }else{
                            top.layer.msg("操作失败！");
                        }
                    }
                });
                layer.close(layer.index);
                var index = parent.layer.getFrameIndex(window.name);
                layer.close(index);
                layui.table.reload('linkListTab');
            });
        }else if (obj.event === 'tableRest') {
            layer.confirm('真的重置此账号密码？？？', function(res) {
                $.ajax({
                    type: "post",
                    url: "/user/restPwd?id=" + data.id,
                    dataType: "json",
                    success: function (res) {
                        if (res.code === 0) {
                            top.layer.msg("操作成功！");
                        } else {
                            top.layer.msg("操作失败！");
                        }
                    }
                });
            });
        }
    });
});